<template>
  <DetailsFormBox :title="page.title" :type="route.query.type" :form="data.form" :btns="[
    {
      title: '重置',
      icon: '',
      type: '',
      hidden: !isEdit,
      click: getForm,
    },
    {
      title: route.query.xieYiShuHao ? '保存' : '仅保存',
      icon: '',
      type: 'primary',
      hidden: !isEdit,
      click: (form: any) => {
        saveBtn(form, false)
      },
    },
    {
      title: '保存并确认',
      icon: '',
      type: 'primary',
      hidden: !isEdit,
      click: (form: any) => {
        saveBtn(form, true)
      },
    },
  ]" :loading="data.loading" :loadingMsg="data.loadingMsg">
    <template #default="{ form }">
      <div class="p-12">
        <OrderInfo :data="data.productOrder" :produceTaskBillNo="route.query.produceTaskBillNo" />
        <CellBox title="基本信息" isTop>
          <FlexBox>
            <ElFormItemBox label="退料单号" prop="backMaterialBillNo">
              <ElInputBox :data="form" dataName="backMaterialBillNo" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="创建人员" prop="createPersonName">
              <ElInputBox :data="form" dataName="createPersonName" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="创建日期" prop="createDate">
              <ElInputBox :data="form" dataName="createDate" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="退料类型" prop="backMaterialType" isRules>
              <ElSelectBox :data="form" dataName="backMaterialType" :options="options.backMaterialType" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="退料备注" prop="backMaterialRemark" class="wAuto">
              <ElInputBox :data="form" dataName="backMaterialRemark" type="textarea" :disabled="!isEdit" />
            </ElFormItemBox>
          </FlexBox>
        </CellBox>
        <CellBox title="单据明细" isTop>
          <GoodList :data="{
            disabled: !isEdit,
            height: 328,
            rowKey: 'stockGoodsCode',
            prop: 'backProduceMaterialMxList',
            tbody: form.backProduceMaterialMxList,
            thead: [
              {
                label: '退料数量',
                prop: 'quantity',
                formType: 'number',
                isRules: true,
              },
              {
                label: '机台名称',
                prop: 'machineCode',
                formType: 'select',
                options: options.machine,
                labelName: 'machineName',
                valueName: 'machineCode',
              },
              {
                label: '备注',
                prop: 'getMaterialRemark',
                formType: 'textarea'
              },
            ],
            operation: [
              {
                label: '删除',
                icon: 'Delete',
              }
            ]
          }" @selectBtn="(rowArr: any) => {
            const _arr = rowArr.map((el: any) => ({
              stockGoodsCode: el.specificationCode, // 库存物料代码
              quantity: null, // 退料数量
              backRemark: '', // 退料备注
              machineCode: '', // 机台编号
            }))
            form.backProduceMaterialMxList.push(..._arr)
          }">
          </GoodList>
        </CellBox>
      </div>
    </template>
  </DetailsFormBox>
</template>
<script setup lang="ts">
import { sendMsg } from '@/utils/crossTagMag.ts';
import { useListComponent } from '../pageTs'; // 引入逻辑
import page from '../page.ts'; // 引入逻辑
import '../pageCss.scss'; // 引入样式
const { dataInfo, options } = useListComponent();
const { proxy } = getCurrentInstance() as any;
const data: any = reactive({
  loading: false,
  loadingMsg: '',
  mainPage: 1,
  form: {
    backMaterialID: 0, // 生产退料主键ID
    backMaterialBillNo: "", // 生产退料单号
    saleOrderNo: "", // 销售订单号
    produceTaskBillNo: "", // 生产任务单号
    getMaterialBillNo: "", // 生产领料单号
    backMaterialType: 1, // 退料类型：1生产退料，其他待定
    backMaterialPerson: "", // 退料人员
    backMaterialRemark: "", // 生产退料备注
    backProduceMaterialMxList: [
      // {
      //   getMxID: 0, // 领料明细主键ID
      //   produceMaterialNo: "", // 生产物料代码
      //   stockGoodsCode: "", // 库存物料代码
      //   backQuantity: 0.0, // 退料数量
      //   backRemark: "", // 退料备注
      //   machineCode: "" // 机台编码
      // }
    ]
  },
});
const isEdit = ref(true)

import { useRoute } from 'vue-router';
// 获取当前路由对象
const route = useRoute();
//获取数据
const getForm = async () => {
  let _query: any = route.query
  setLoading()
  //获取工单信息
  //是否新建
  if (!!!_query.backMaterialBillNo) {
    if (!!_query.produceTaskBillNo) {
      proxy.$api.ProductionBusniess.GetProduceTaskBillDetail({
        produceTaskBillNo: _query.produceTaskBillNo
      })
        .then((res: any) => {
          data.form.backProduceMaterialMxList = res.produceMaterialList.map((el: any) => ({
            stockGoodsCode: el.stockGoodsCode, // 库存物料代码
            quantity: null, // 退料数量
            machineCode: el.equipmentNo, // 机台编码
            backRemark: el.backRemark || '', // 备注
          }))
          data.form.saleOrderNo = res.baseData.saleOrderGroupNumber//订单单号
          data.form.produceTaskBillNo = res.baseData.produceTaskBillNo//生产单号
          data.loading = false
        })
        .catch(() => {
          data.loading = false
        });
    } else {
      data.loading = false
    }
    return
  }
  proxy.$api.ProductionBusniess.GetBackProduceMaterialBillDetail({
    backMaterialBillNo: _query.backMaterialBillNo,
  })
    .then((res: any) => {
      proxy.$message('获取成功')
      isEdit.value = (['1', '2'].includes(_query.type) && res.baseData.backMaterialBillState === 0)
      data.form = res.baseData
      data.loading = false
    })
    .catch(() => {
      data.loading = false
    });
};
const saveBtn = (form: any, isConfirm: boolean = false) => {
  const _form: any = {
    saleOrderNo: form.saleOrderNo, // 销售订单号
    produceTaskBillNo: form.produceTaskBillNo, // 生产任务单号
    backMaterialID: form.backMaterialID, // 生产退料主键ID
    backMaterialBillNo: form.backMaterialBillNo, // 生产退料单号
    getMaterialBillNo: form.getMaterialBillNo, // 	生产领料单号
    backMaterialType: form.backMaterialType, // 退料类型：1生产退料，其他待定
    backMaterialPerson: form.backMaterialPerson, // 退料人员
    backMaterialRemark: form.backMaterialRemark, // 领料备注
    mxDataList: form.backProduceMaterialMxList.map((el: any) => (
      {
        stockGoodsCode: el.stockGoodsCode, // 库存物料代码
        backQuantity: el.quantity, // 退料数量
        machineCode: el.machineCode, // 机台编码
        backRemark: el.backRemark || '', // 备注
      }
    ))
  }
  if (_form.backMaterialBillNo) {
    proxy.$api.ProductionBusniess.UpdateBackProduceMaterialBill(_form)
      .then((res: any) => {
        if (isConfirm) {
          _setState(_form)
        } else {
          _sun()
        }
      })
      .catch(() => { });
  } else {
    proxy.$api.ProductionBusniess.CreateBackProduceMaterialBill(_form)
      .then((res: any) => {
        _form.backMaterialBillNo = res.backMaterialBillNo
        _form.backMaterialID = res.backMaterialID
        if (isConfirm) {
          _setState(_form)
        } else {
          _sun()
        }
      })
      .catch(() => { });
  }

  function _sun() {
    proxy.$message('保存成功')
    sendMsg('save', true);
    // getForm()
    window.close();
  }
  function _setState(form: any) {
    proxy.$api.ProductionBusniess.SureBackProduceMaterialBill({
      backMaterialID: form.backMaterialID
    })
      .then((res: any) => {
        _sun()
      })
      .catch(() => { });
  }
};
const setLoading = (title: string = '正在获取数据,请稍等...') => {
  if (!data.loading) {
    data.loading = true;
    data.loadingMsg = title;
  }
};
import { getMachine } from '@/utils/optionsArr.ts';
onMounted(() => {
  getForm();
  getMachine(options, 'machine')//获取设备
});
</script>
